﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace DailyBread.Views
{
    public partial class SettingPage : PhoneApplicationPage
    {
        public App currentApp { get; set; }

        public SettingPage()
        {
            InitializeComponent();
            currentApp = ((App)Application.Current);
            fontsizeSlider.Value = currentApp.setting.TextFontSize * 2;
            bgShadeSlider.Value = currentApp.setting.BGShade * 10;
            SampleText.FontSize = currentApp.setting.TextFontSize;
            SampleText.Text = "John 3:16 For God so loved the world that he gave his one and only Son " + 
                                "that whoever believes in him shall not perish but have eternal life." +
                                "17 For God did not send his Son into the world to condemn the world, but to save the world through him." +
                                "18 Whoever believes in him is not condemned, but whoever does not believe stands condemned already " + 
                                "because they have not believed in the name of God’s one and only Son. " ;


            if (currentApp.setting.LightBG == true)
            { // when light background is set, always use it
                UseSolidBG.Visibility = Visibility.Collapsed;
                LayoutRoot.Background = App.Current.Resources["PhoneBackgroundBrush"] as Brush;
            }
            else
            {
                UseSolidBG.IsChecked = currentApp.setting.UseSolidBG;
                if (UseSolidBG.IsChecked == true)
                {
                    LayoutRoot.Background = App.Current.Resources["PhoneBackgroundBrush"] as Brush;
                }
                else
                {
                    PanoramaBG.GetBingImage(
                        delegate(Uri imgUri)
                        {
                            LayoutRoot.Background = new ImageBrush
                            {
                                ImageSource = new BitmapImage(imgUri)
                            };
                        }
                    );
                }
            }
            fontsizeSlider.ValueChanged += fontsizeSlider_ValueChanged;
            bgShadeSlider.ValueChanged += bgShadeSlider_ValueChanged;
        }

        private void fontsizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            currentApp.setting.TextFontSize = e.NewValue / 2;
            SampleText.FontSize = e.NewValue / 2;
        }

        private void bgShadeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            ResourceDictionary res = App.Current.Resources;
            SolidColorBrush bgBrush = res["TransparentBackground"] as SolidColorBrush;
            bgBrush.Opacity = e.NewValue / 10;
            currentApp.setting.BGShade = e.NewValue / 10;
        }

        private void UseSolidBGHandleCheck(object sender, RoutedEventArgs e)
        {
            
            //SolidColorBrush b = new SolidColorBrush(Color.FromArgb(255, 0,255,0));
            //LayoutRoot.Background = b;
            LayoutRoot.Background = App.Current.Resources["PhoneBackgroundBrush"] as Brush;
            currentApp.setting.UseSolidBG = true;
            currentApp.setting.BGSettingChanged = true;
        }

        private void UseSolidBGHandleUnchecked(object sender, RoutedEventArgs e)
        {
            PanoramaBG.GetBingImage(
                delegate(Uri imgUri)
                {
                    LayoutRoot.Background = new ImageBrush
                    {
                        ImageSource = new BitmapImage(imgUri)
                    };
                }
            );
            //LayoutRoot.Background = new ImageBrush
            //{
            //    ImageSource = new BitmapImage(new Uri("images/sunset.jpg", UriKind.Relative))
            //};
            currentApp.setting.UseSolidBG = false;
            currentApp.setting.BGSettingChanged = true;
        }
    }
}